package com.icow.wms.basic.domain;

import com.icow.wms.basic.utils.PLCUtils;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Slf4j
public class SsxFinish implements Runnable {
    private String ip;
    private String finish;
    private String finishAck;

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
                boolean finishData = PLCUtils.writePLC(ip, finish, true);
                if (finishAck == null || finishAck.equals("")) {
                    Thread.sleep(1000 * 6);
                    if (finishData) {
                        boolean finishClearData = PLCUtils.writePLC(ip, finish, false);
                        if (finishClearData) {
                            log.info("完成无反馈对接结束 finishData:" + finishData + " finishClearData:" + finishClearData);
                            return;
                        }
                    }
                } else {
                    boolean finishAckData = PLCUtils.readPLC(ip, finishAck);
                    if (finishAckData) {
                        boolean finishClearData = PLCUtils.writePLC(ip, finish, false);
                        if (finishClearData) {
                            log.info("完成有反馈对接结束 finishData:" + finishData + " finishClearData:" + finishClearData);
                            return;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.info("完成对接异常" + e.getMessage());
            }
        }
    }
}
